#!/bin/bash

install ()
{
    echo 'Installing...'
    cd "$(dirname $0)"
    mkdir -p ~/catin
    if [ "$(pwd)" != "${HOME}/catin" ]; then
        for file in $(ls -A); do
            if [ -d "$file" ] && [ "$file" != ".git" ] ; then
                ln -sf "$(pwd)/$file" ~/catin/
            fi
        done
        for file in $(ls *.py); do
            ln -sf "$(pwd)/$file" ~/catin/
        done
        ln -sf "$(pwd)/install.sh" ~/catin/
        ln -sf "$(pwd)/catin.config" ~/catin/
    fi
    mkdir -p ~/.config/systemd/user
    cp catin.timer ~/.config/systemd/user
    cp catin.service ~/.config/systemd/user
    systemctl --user daemon-reload
    systemctl --user enable catin.timer
    systemctl --user start catin.timer

    echo 'Installed.'
    echo 'Run "systemctl --user disable catin.timer" to disable the service'
    echo 'Run "systemctl --user stop catin.timer" to stop the service'
}

uninstall()
{
    echo 'Uninstalling...'
    systemctl --user stop catin.timer
    systemctl --user disable catin.timer
    rm ~/.config/systemd/user/catin.timer
    rm ~/.config/systemd/user/catin.service
    rm -rf ~/.config/catin
    systemctl --user daemon-reload
    rm -rf ~/catin
    echo 'Uninstalled'
}
help()
{
    echo 'Usage: ./install.sh [-u|--uninstall]'
    echo 'Options:'
    echo '  -u, --uninstall    Uninstall the service'
    echo '  -h, --help         Show this help message'
}

if [ "$1" == "-u" ] || [ "$1" == "--uninstall" ]; then
    uninstall
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
    help
else
    install
fi

